package com.boot_reggie.filter;

import com.alibaba.fastjson.JSON;
import com.boot_reggie.common.Result;
import com.boot_reggie.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.logging.LogRecord;
@Slf4j
@WebFilter(filterName = "LoginFilter",urlPatterns = "/*")
public class LoginFilter implements Filter {
//    路径匹配器
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse res = (HttpServletResponse) servletResponse;
        System.out.println("=====>"+req.getMethod());
        System.out.println(req.getMethod().equals("OPTIONS"));
        if(req.getMethod().equals("OPTIONS")){
            filterChain.doFilter(req,res);
            return;
        }
//        res.setHeader("Access-Control-Allow-Credentials","true"); //是否支持cookie跨域
        log.info("拦截到请求:{}",req.getRequestURI());
//        定义不需要处理的路径
        String [] list = new String[]{
                "/admin/login",
                "/admin/test",
                "/dishes/dish",
                "/common/download",
                "/dishes/byId",
                "/Packages/Setmeal/*",
                "/code",
                "/common/upload",
                "/user",
                "/code/login",
                "/user/login",
                "/user/test",

                "/user/exit",
                "/order/getPage",
                "/payment/pay",
                "/payment/notify",


                "/",
                "/src/**",
                "/fonts/**",
                "/img/**",
                "/js/**"
        };

//      判断此次请求是否需要处理
        boolean data = check(req.getRequestURI(),list);
        if(data){
//            如果不需要拦截直接放行
            filterChain.doFilter(req,res);
            return;
        }

//        判断是否登录
        if(req.getSession().getAttribute("employee") != null || req.getSession().getAttribute("userInfo") != null){
            if(req.getSession().getAttribute("employee") != null){
                BaseContext.setCurrentID((Long) req.getSession().getAttribute("employee"));
                System.out.println(req.getSession().getAttribute("userInfo"));
            }
            if(req.getSession().getAttribute("userInfo") != null){
                User user = (User) req.getSession().getAttribute("userInfo");
                System.out.println("用户session====================="+user);
                BaseContext.setCurrentID(user.getId());
                BaseContext.setThreadLocal1(user.getId());
            }



            filterChain.doFilter(req,res);
            return;

        };



//        返回错误信息
        res.setCharacterEncoding("UTF-8"); //转换编码防止乱码
        res.getWriter().write(JSON.toJSONString(Result.Nulllogin()));
    }
//    路径匹配方法
    public boolean check (String reqURL,String[] list){
        for (String s : list) {
            boolean data = PATH_MATCHER.match(s,reqURL);
            if(data){
                return true;
            };
        }
        return false;
    }
}
